package com.quanxiaoha.weblog.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quanxiaoha.weblog.admin.model.vo.category.AddCategoryReqVO;
import com.quanxiaoha.weblog.admin.model.vo.category.DeleteCategoryReqVO;
import com.quanxiaoha.weblog.admin.model.vo.category.FindCategoryPageListRspVO;
import com.quanxiaoha.weblog.admin.model.vo.category.GetCategoryPageListReqVO;
import com.quanxiaoha.weblog.admin.model.vo.tag.*;
import com.quanxiaoha.weblog.admin.service.AdminCategoryService;
import com.quanxiaoha.weblog.admin.service.AdminTagService;
import com.quanxiaoha.weblog.common.domain.dos.CategoryDO;
import com.quanxiaoha.weblog.common.domain.dos.TagDO;
import com.quanxiaoha.weblog.common.domain.mapper.CategoryMapper;
import com.quanxiaoha.weblog.common.domain.mapper.TagMapper;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import com.quanxiaoha.weblog.common.exception.BizException;
import com.quanxiaoha.weblog.common.model.vo.SelectRspVO;
import com.quanxiaoha.weblog.common.utils.PageResponse;
import com.quanxiaoha.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;


    /**
     * 批量插入标签信息
     *
     * @param addTagReqVO
     * @return
     */
    public Response addTags(AddTagReqVO addTagReqVO) {
        //VO转DO
        List<TagDO> tagDOS = addTagReqVO.getTagName().stream()
                .map(TagName -> TagDO.builder()
                        .name(TagName.trim())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build())
                .collect(Collectors.toList());
        //调用mp中的批量插入方法
        try {
            saveBatch(tagDOS);
        } catch (Exception e) {
            //捕获标签内容重复异常，只添加内容不重复的标签
            log.warn("标签内容重复添加", e);
        }
        return Response.success();
    }

    /**
     * 查询标签分页数据
     *
     * @param getTagPageListReqVO
     * @return
     */
    public PageResponse getTagPageList(GetTagPageListReqVO getTagPageListReqVO) {
        // 获取当前页、以及每页需要展示的数据数量
        Long current = getTagPageListReqVO.getCurrent();
        Long size = getTagPageListReqVO.getSize();

        // 分页对象(查询第几页、每页多少数据)
        Page<TagDO> page = new Page<>(current, size);

        String name = getTagPageListReqVO.getName();
        LocalDate startDate = getTagPageListReqVO.getStartDate();
        LocalDate endDate = getTagPageListReqVO.getEndDate();

        Page<TagDO> tagDOPage = tagMapper.selectPageList(page, name, startDate, endDate);

        List<TagDO> tagDOS = tagDOPage.getRecords();
        // DO 转 VO
        List<FindTagPageListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(tagDOS)) {
            vos = tagDOS.stream()
                    .map(tagDO -> FindTagPageListRspVO.builder()
                            .id(tagDO.getId())
                            .name(tagDO.getName())
                            .createTime(tagDO.getCreateTime())
                            .build())
                    .collect(Collectors.toList());
        }

        return PageResponse.success(tagDOPage, vos);
    }

    @Override
    public Response deleteTag(DeleteTagReqVO deleteTagReqVO) {
        Long id = deleteTagReqVO.getId();
        int i = tagMapper.deleteById(id);
        return i == 1 ? Response.success() : Response.fail(ResponseCodeEnum.TAG_NOT_EXIT);
    }

    @Override
    public Response searchTags(SearchTagReqVO searchTagReqVO) {
        //获取关键字
        String key = searchTagReqVO.getKey();
        //条件构造器
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();
        //like + 创建时间倒序
        wrapper.like(TagDO::getName, key).orderByDesc(TagDO::getCreateTime);
        //获取到对应列表
        List<TagDO> tagDOList = tagMapper.selectList(wrapper);
        //do 转 vo

        //使用下拉列表通用vo  selectRspVo
        List<SelectRspVO> vos = null;
        //stream流转化
        if (!CollectionUtils.isEmpty(tagDOList)) {
            vos = tagDOList.stream()
                    .map(tagDO -> SelectRspVO.builder()
                            .label(tagDO.getName())
                            .value(tagDO.getId())
                            .build())
                    .collect(Collectors.toList());
        }

        //返回结果
        return Response.success(vos);
    }
}
